<?php
namespace Home\Controller;
use Think\Controller;
class ChoujController extends CommonController {
	//抽奖
    public function index()
    {
        //随机红包
        $arr = array(10,20,30,50);
        $arr_num = array_rand($arr,1);
        $this->assign('num',$arr[$arr_num]);
        

        if (!$_SESSION['id']) {
            $this->redirect('user/login');
            exit;
        }
        
        $uid = $_SESSION['id'];
        //本次抢红包金额
        $hongbao = M('user_price')->where(array('uid'=>$uid))->find();
        $this->assign('hongbao',$hongbao);

        //更新 第二天拆红包
        $time = time(); 
        $date_time = date('Y-m-d',$time);//当日
        $dr_time = strtotime($date_time);
        $this->assign('dr_time',$dr_time);
        

        $this->display();
    }

    public function cj_num_add()
    {//Array ( [cj_num] => 10 
        if (IS_POST) {
            $data['price'] = I('post.cj_num');
            $data['hb_time'] = time();

            $uid = $_SESSION['id'];

            $hongbao = M('user_price')->where(array('uid'=>$uid))->find();
            if (!$hongbao) {
                //添加
                $data['uid'] = $uid;
                $data['is_who'] = $_SESSION['is_who'];
                //第一次拆红包放入总金额
                $data['z_price'] = $data['price'];
                

                $res = M('user_price')->add($data);
                    if ($res) {
                        $arr = array(
                            'error' => 1,
                            //'info' => '成功'    
                            );
                        echo json_encode($arr);
                    }else {
                        $arr = array(
                            'error' => 0,
                            'info' => '拆红包失败,重新拆'    
                            );
                        echo json_encode($arr);
                    }
             }else {
                //更新 第二天拆红包
                $data['z_price'] = $hongbao['z_price'] + $data['price'];

                $res = M('user_price')->where(array('id'=>$hongbao['id']))->save($data);;
                if ($res) {
                    $arr = array(
                        'error' => 1,
                        //'info' => '成功'    
                        );
                    echo json_encode($arr);
                }else {
                    $arr = array(
                        'error' => 0,
                        'info' => '拆红包失败,重新拆'    
                        );
                    echo json_encode($arr);
                }

             }
        }
        
    }


    //
    public function danqian_yue()
    {
        $time = time(); 
        echo $time;                            //时间戳
        echo '<br>';

        $date_time = date('Y-m-d',$time);      //当日
        $date_math = date('Y-m',$time);      //当月
        echo $date_time;
        echo '<br>';
        echo $date_math;
        echo '<br>';

        //echo date('w',strtotime($date_time));  //当日周几
        echo '<br>';

        $days = date("t");                     //单月总天数
        echo $days;
        echo '<br>';


        $yue_arr = array();                    //当月每天存入数组
        for ($i=1; $i <= $days ; $i++) { 
            //$yue_arr[]['date_num'] = $i;
            $yue_arr[]['date'] = $date_math.'-'.$i;
        }
        $this->assign('yue_arr',$yue_arr);
        print_r($yue_arr);
        echo '<br>';

        $zhouyi = array();
        
        foreach ($yue_arr as $v) {
            $dangq_zj = date('w',strtotime($date_math.'-'.$v['date_num']));
            print_r($dangq_zj);

            if ($v['date_num'] == $dangq_zj) {
                echo $v['date_num'];
            }
        }

        $this->display();

    }

    //活动页面
    public function huodong()
    {
        $this->display();
    }


}